<?php
namespace Api\Controller;
use Think\Controller;
use Think\Log;
/*
 * api类都继承此类
 */
class CommonController extends Controller {
	
	public function __construct() {
		parent::__construct();
		$this->_init();
	}
	
	/**
	 * 初始化
	 * @return
	 */
	 
	 
	 private function _init() {
	 	
	 	$isOk = $this->cheakToken();
		if(!$isOk){
			//没有传appid和key
			return apishow('没有key和id');
		}else{
			if($isOk==1){
				return true;
			}else{
				return apiShow($isOk,110);
			}
		}
	 }
	 
	 
	 /**
	  * 效验 key id
	  * @return boolean
	  */
	 public function cheakToken(){
	 	$data = $_POST;
		if($data && is_array($data)){


					if(!$data['app_id']){
					    $this->error(1001);
//						return '缺少app_id';
					}else{
						if($data['app_id']==C('APP_ID')){
                            $sign = $data['sign'];
                            $time = strtotime($data['timestamp']);
                            $timediff = time() - $time;
                            if ($timediff > 60) {
                                $this->error(202);
//                                return '请求非法（时间）';
                            }

                            if(!$sign){
                                $this->error(1003);
//                                return '签名缺失';
                            }
                            if ($this->getSign($data) != $sign) {
                                $sings =$this->getSign($data);
                                $this->error(1004);
//                                return "签名不正确" . $sings;
                            }
							return 1;
						}else{
						    $this->error(1002);
//							return 'app_id错误';
						}
					}


			
			
			
		}
		return false;
	 }

    /**
     * 生成Token
     * @author suo
     * @email 774303486@qq.com
     */

    public function getToken($params)
    {
        return $this->getSign($params);
    }

    /**
     * 从前端的参数中生成签名
     * @param  array $params 待签名参数
     * @return string
     */
    protected function getSign($params)
    {
        unset($params['sign']);
        if (isset($params['callback'])) {
            unset($params['callback'], $params['_']);
        }
        ksort($params);
        $tmps = array();
        foreach ($params as $k => $v) {
            $tmps[] = $k . $v;
        }
        $string = implode('', $tmps) . C('APP_SECRET');
        Log::record($string);
//        return $string;
        return strtolower(hash('md5', $string));
    }





    /**
     * @annotate 返回错误
     * @author suo
     * @return json
     */
    public function error($code = 0)
    {
        if (is_integer($code)) {
            $data = [
                'status' => false,
                'code' => $code,
                'msg' => $this->getError($code)
            ];
        } else {
            $data = [
                'status' => false,
                'code' => 1000,
                'msg' => $code
            ];
        }

        // 返回JSON数据格式到客户端 包含状态信息
        header('Content-Type:text/html; charset=utf-8');

        exit(json_encode($data));
    }


    public function getError($code = 0)
    {
        $error_code = C("ERRORCODE");
        return $error_code[$code];
    }



	
}